$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'xm/customer/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '客户姓名', name: 'customerName', index: 'customer_name', width: 80},
            {label: '客户手机号码', name: 'customerTel', index: 'customer_tel', width: 80},
            {label: '客户邮箱', name: 'customerEmail', index: 'customer_email', width: 80},
            {label: '会员卡号', name: 'vipNo', index: 'vup_no', width: 80},
            {label: '账户金额', name: 'payBalance', index: 'pay_balance', width: 80}
            // ,
            // { label: '备注', name: 'remark', index: 'remark', width: 80 },
            // { label: '创建时间', name: 'createTi', index: 'create_ti', width: 80 },
            // { label: '创建人', name: 'createUserId', index: 'create_user_id', width: 80 },
            // { label: '修改时间', name: 'modifyTi', index: 'modify_ti', width: 80 },
            // { label: '修改人', name: 'modifyUserId', index: 'modify_user_id', width: 80 },
            // { label: '版本号', name: 'version', index: 'version', width: 80 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        addList: false,
        payList: false,
        rechargeList: false,
        zengsongList:false,
        title: null,
        customer: {},
        proList :null,
        workerList:null,
        msg :null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.addList = true;
            vm.title = "新增";
            vm.customer = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.rechargeList = false;
            vm.addList = true;
            vm.title = "修改";
            vm.getInfo(id)
        },
        pay: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.payList = true;
            vm.showList = false;
            vm.title = "消费";
            vm.getInfo(id)
            vm.msg = "";


          $.get(baseURL + "xm/prodcategory/select", function (r) {
            vm.proList = r.list;
          });

          $.get(baseURL + "xm/masterworker/select", function (r) {
            vm.workerList = r.list;
          });


        },
        recharge: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.payList = false;
            vm.rechargeList = true;
            vm.title = "充值";
            vm.getInfo(id)
        },
        zengsong: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.payList = false;
            vm.rechargeList = false;
            vm.zengsongList =true;
            vm.title = "赠送";
            vm.getInfo(id);

            $.get(baseURL + "xm/prodcategory/select", function (r) {
                vm.proList = r.list;
            });
        },
        zsUpdate: function (event) {
            if(!vm.customer.productId){
                layer.msg("赠送项目必须选择");
                return;
            }
            if(!vm.customer.zengsong){
                layer.msg("赠送次数不能为空");
                return;
            }

            var url = "xm/customer/zengsong";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.customer),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        firstChange: function (event) {
            var id = getSelectedRow(),jieguo,
                yue= vm.customer.payBalance,
                xiaoFeiId = event.target.value,
                danjia = $("#first option:selected").attr("data-ins"),
                vip = $("#first option:selected").attr("data-vip");

            if(vip > 0){
                jieguo = yue -vip;
            }else{
                jieguo = yue -danjia;
            }

            vm.msg=null;

            if(jieguo < 0){
                vm.msg = "充值金额不足,请重新选择消费项目";
                layer.msg("充值金额不足,请重新选择消费项目");
            }

        },
        rechargeMoney: function (event) {
            if(!vm.customer.balance){
                layer.msg("充值金额不能为空");
                return false;
            }
          var num =vm.customer.payBalance+vm.customer.balance;
          var msg ="充值前余额："+vm.customer.payBalance+"元;<br/>";
          msg = msg+ "本次充值金额："+vm.customer.balance+"元;<br/>";
          msg = msg+ "充值后总金额："+num+"元;<br/>";


          layer.confirm(msg, {title:'确认要充值吗？'}, function(index){
            layer.close(index);
            var url = vm.customer.id == null ? "" : "xm/customer/recharge";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.customer),
                success: function (r) {
                    if (r.code === 0) {

                      layer.msg('操作成功',{
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                      }, function (index) {
                          vm.reload();
                          layer.close(index);
                      });
                    } else {
                      layer.alert(r.msg);
                    }
                }
            });

          });

        },
        payMoney: function (event) {
            if(vm.msg){
                layer.msg(vm.msg);
                return ;
            }

            if(!vm.customer.productId){
                layer.msg("请选择消费项目");
                return ;
            }

          var jieguo,yue= vm.customer.payBalance,
              danjia = $("#first option:selected").attr("data-ins"),
              vip = $("#first option:selected").attr("data-vip");

            if(vip > 0){
                danjia = vip;
            }

            jieguo = yue - danjia;


          var msg ="消费前余额："+yue+"元;<br/>";
          msg = msg+ "本次消费金额："+danjia+"元;<br/>";
          msg = msg+ "消费后余额："+jieguo+"元;<br/>";


          layer.confirm(msg, {title:'确认要消费吗？'}, function(index){
            layer.close(index);

            var url = vm.customer.id == null ? "" : "xm/customer/pay";
            $.ajax({
              type: "POST",
              url: baseURL + url,
              contentType: "application/json",
              data: JSON.stringify(vm.customer),
              success: function (r) {
                if (r.code === 0) {

                  layer.msg('操作成功',{
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                  }, function (index) {
                    vm.reload();
                    layer.close(index);
                  });
                } else {
                  layer.alert(r.msg);
                }
              }
            });






          });


        },
        saveOrUpdate: function (event) {
            var url = vm.customer.id == null ? "xm/customer/save" : "xm/customer/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.customer),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "xm/customer/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "xm/customer/info/" + id, function (r) {
                r.customer.workId = 1;
                r.customer.productId = "";
                vm.customer = r.customer;
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.addList = false;
            vm.payList = false;
            vm.rechargeList = false;
            vm.zengsongList = false;

            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});